package pattern;

import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import pattern.i18n.I18nMessage;
import pattern.config.FeignInterceptor;
import pattern.socket.SocketServerConfigurer;

import javax.annotation.Resource;

/**
 * socket启动入口
 * @author Simon

 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableCreateCacheAnnotation
@EnableMethodCache(basePackages = "pattern.cache")
@Slf4j
@Controller
@EnableScheduling
@EnableDiscoveryClient
@EnableFeignClients
public class SocketApplication extends WebMvcConfigurationSupport {

    @Resource
    private FeignInterceptor feignInterceptor;

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        SpringApplication application = new SpringApplication(SocketApplication.class);
        application.run(args);
        log.info("启动总耗时间{}ms", System.currentTimeMillis() - start);
    }

    /**
     * 系统资源访问
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/session/**");
    }

    /**
     * 登陆及权限拦截
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("初始化会话拦截器");
        //注入微服务会话
        registry.addInterceptor(feignInterceptor)
                .addPathPatterns("/**");
    }


    @RequestMapping(value = "/")
    public String index() {
        //客户端用于测试socket连接
        String local = I18nMessage.getMessage("locale");
        log.info(local);
        return "index";
    }


}

